<?php


namespace app\api\controller;

use app\admin\model\Tour as ToursModel;

class Tour extends Controller
{

    public function index(){
//        var_dump(session('username'));
        $file = request()->file('tour_picture');
        session_start();
        $name = $_SESSION['n'] ;
        $model = new ToursModel();
        $users = $model->getList($name);
        $this->assign('tour_id', $users);
        return $this->renderSuccess($users, '获取数据成功');
    }
    public function detail($id)
    {
        $model = new ToursModel();
        $emp = $model->where(['tour_id' => $id])->find();
        return $this->renderSuccess($emp, '获取员工数据成功');
    }

    public function doadd(){
        $model = new ToursModel();
        if ($model->save($this->request->param())){
            $tour = $model->where(['tour_id' => $model->getLastInsID()])->find();
            return $this->renderSuccess($tour, '添加成功');
        }else{
            return $this->renderError('添加失败');
        }
    }
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if($info){
                // 成功上传后 获取上传信息, 输出 jpg
                $upload_dir = request()->domain() . request()->root(); //http://localhost/tp/hr_webproj/service/public/index.php
                $upload_dir = substr($upload_dir, 0, strlen($upload_dir) - 9).'upload/';  //去掉index.php

                echo json_encode(['errno'=> 0, 'data' => [$upload_dir.$info->getSaveName()]]);
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }

    public function upload_pri(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if($info){
                // 成功上传后 获取上传信息, 输出 jpg
                echo json_encode(['errno'=> 0, 'data' => [
                    'path' => fixed_url(). 'upload/'.$info->getSaveName(),
                    'name' => $info->getSaveName()
                ]]);
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }

    public function edit($no){
        //echo $no;
        //取出员工部门
        $model=new toursModel();
        $tours=$model->getTours($no);
//        var_dump($res);
        //数据输出
        return $this->fetch('edit',['tours'=>$tours]);
        //      方法二  return $this->fetch('edit',['emp'=>$emp,'depts'=>$depts]);
    }

    public function doedit(){
        $data=$this->request->param();
//        var_dump($data);
        $model=new ToursModel();
        $res= $model->updateTours($data);

        if($res){
            $this->success('修改成功','tours/index');
        }else{
            $this->error('修改失败','index');
        }
    }

    public function del($no){
        $model=new ToursModel();
        if($model->delTours($no)){
            $this->success('删除成功','tours/index');
        }else{
            $this->error('删除失败','index');
        }
    }

}
